package com.zk.nakedfowl.powerManager.controller;


import com.zk.nakedfowl.common.entity.Result;
import com.zk.nakedfowl.common.entity.StatusCode;
import com.zk.nakedfowl.common.util.JwtUtil;
import com.zk.nakedfowl.powerManager.pojo.User;
import com.zk.nakedfowl.powerManager.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class LoginController {

    @Autowired
    private UserService userService;
    @Autowired
    private JwtUtil jwtUtil;

    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public Result login(String loginName,String password){
        Map map1=new HashMap();
        map1.put("loginName",loginName);

       List<User>  users=userService.findSearch(map1);
        if(users!=null){
            User user=users.get(0);
            if (user.getPassword()==password) {
                //登录名如果是中文会乱码
                String jwtToken=jwtUtil.createJWT(user.getId(),user.getLoginName(),user.getRole());
                Map<String,Object> map = new HashMap<>();
                map.put("token",jwtToken);
                map.put("role",user.getRole());

                map.put("loginName",user.getLoginName());
                map.put("userId",user.getId());
                return new Result(true,StatusCode.OK,"登录成功",map);

            }
        }
        return new Result(false, StatusCode.LOGINERROR,"用户名或密码错误");
    }


}
